/*******************************************************************************
 * Copyright (c) 2010, 2018 西安秦晔信息科技有限公司
 * Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *******************************************************************************/
package com.qinyeit.configuration;

import com.qinyeit.webapp.security.filter.StatelessAuthenticationFilter;
import com.qinyeit.webapp.security.filter.StatelessRequestSignValidationFilter;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.spring.config.web.autoconfigure.ShiroWebFilterConfiguration;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.servlet.Filter;
import java.util.Map;

/**
 * <p>ClassName: com.qinyeit.shirostarterdemo.configuration.ShiroFilterConfiguration
 * <p>Function: shiro过滤器配置
 * <p>date: 2018-08-13 16:19
 *
 * @author wuqing
 * @version 1.0
 * @since JDK 1.8
 */
@Slf4j
@Configuration
public class ShiroFilterConfiguration extends ShiroWebFilterConfiguration {
    @Autowired
    private Environment environment;
    @Override
    protected ShiroFilterFactoryBean shiroFilterFactoryBean() {
        ShiroFilterFactoryBean filterFactoryBean = super.shiroFilterFactoryBean();
        registFilter(filterFactoryBean.getFilters());
        return filterFactoryBean;
    }
    /**
     * 注册过滤器
     * @return
     */
    private void  registFilter(Map<String,Filter> filterMap){
        StatelessAuthenticationFilter filter=new StatelessAuthenticationFilter();
        filter.setUsernameParam("account");//账号字段为account

        filter.setLoginUrl("/management/signin");//设置登录地址
        //如果是生产环境,打开图片验证码
        if(environment.acceptsProfiles("production")){
            filter.setJcaptchaEbabled(true);
        }
        StatelessRequestSignValidationFilter signValidationFilter=new StatelessRequestSignValidationFilter();

        filterMap.put("authc",filter);
        filterMap.put("signv",signValidationFilter);
    }

}
